<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>

    <link rel="icon" href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/favicon.ico">
    <link rel="canonical" href="https://getbootstrap.com/docs/3.4/examples/justified-nav/">

    <!-- Bootstrap core CSS -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/css/ie10-viewport-bug-workaround.css"
          rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/examples/justified-nav/justified-nav.css"
          rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]>
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/js/ie8-responsive-file-warning.js"></script>
    <![endif]-->
    <script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/js/ie-emulation-modes-warning.js"></script>


</head>
<body>
<div align="center">
    <h1>注册页面</h1>
</div>
<form class="form-horizontal">
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">会员名称</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="username" id="username"/>
        </div>
    </div>
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">密码</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="password" id="password"/>
        </div>
    </div>
    <div class="form-group">
        <label for="username" class="col-sm-2 control-label">重复密码</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="repassword" id="repassword"/>
        </div>
    </div>
    <div class="form-group">
        <label for="email" class="col-sm-2 control-label">会员邮箱</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="email" id="email"/>
        </div>
    </div>
    <div class="form-group">
        <label for="telephone" class="col-sm-2 control-label">联系电话</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="telephone" id="telephone"/>
        </div>
    </div>

    <div class="form-group">
        <label for="telephone" class="col-sm-2 control-label">性别</label>
        <div class="col-sm-9">
            <label class="radio-inline">
                <input type="radio" name="gender" value="1"> 男
            </label>
            <label class="radio-inline">
                <input type="radio" name="gender" value="0"> 女
            </label>
        </div>
    </div>
    <div class="form-group">
        <label for="telephone" class="col-sm-2 control-label">个人介绍</label>
        <div class="col-sm-9">
            <textarea type="text" class="form-control" name="introduction" id="introduction"></textarea>
        </div>
    </div>
    <div align="center">
        <button type="button" class="btn btn-lg btn-success" id="btn_submit">提交</button>
        <button class="btn btn-lg btn-primary" type="reset">重置</button>
        <a class="btn btn-lg btn-default" href="../index.html">返回</a>
    </div>

</form>

<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
        crossorigin="anonymous"></script>
<script>
    window.jQuery || document.write(
        '<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/js/vendor/jquery.min.js"><\/script>'
    )
</script>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/js/docs.min.js"></script>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="https://cdn.jsdelivr.net/npm/@bootcss/v3.bootcss.com@1.0.15/assets/js/ie10-viewport-bug-workaround.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/sweetalert/2.1.2/sweetalert.min.js" type="text/javascript"></script>
<script type="text/javascript">
    var messageAlert = function (title, text, icon, flag, fun) {
        swal({
            title: title,
            text: text,
            icon: icon,
            button: "确定",
        }).then(fun);
        return flag;
    };
    $(function () {
        var $username = $("#username");
        var $password = $("#password");
        var $repassword = $("#repassword");
        var $email = $("#email");
        var $telephone = $("#telephone");
        var $introduction = $("#introduction");
        var $sub = $("#btn_submit");
        var n = /^\w{4,16}$/
        var e = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        $sub.click(function () {
            var $sex = $("[name='gender']:radio:checked");

            if (!n.test($username.val())) {
                return messageAlert("错误", "用户名长度不正确", "error", false)
            }
            if (!n.test($password.val())) {
                return messageAlert("错误", "密码长度不正确", "error", false)
            }
            if ($password.val() !== $repassword.val()) {
                return messageAlert("错误", "密码不一致", "error", false)
            }
            if (!e.test($email.val())) {
                return messageAlert("错误", "邮箱格式不正确", "error", false)
            }
            if ($sex.val() === undefined) {
                return messageAlert("错误", "性别没选", "error", false)
            }


            $.ajax({
                url: "http://localhost:8080/bookStore2/user/servlet.do",
                type: "GET",

                data: {
                    action:"register",
                    email: $email.val(),
                    username: $username.val(),
                    password: $password.val(),
                    telephone: $telephone.val(),
                    sex: $sex.val(),
                    introduction: $introduction.val(),
                    repassword:$repassword.val()
                },
                success: function (data) {
                    if (data==="1"){
                        messageAlert("正确","注册成功","success",true,function () {
                            location.href="./login.html"
                        })
                    }else {
                        return messageAlert("错误", "注册失败", "error", false)
                    }
                },
                dataType: "text"
            });
        })
    });

</script>
</body>
</html>